/**
 * Copyright 2008 floop.pl 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.model;

import java.util.Arrays;

/**
 * Holds the event data
 * 
 * @author floop.pl
 */
public class Event {
	public final static int	EVENT_T_TIMER			= 0;
	public final static int	EVENT_T_TIMER_OOC		= 1;
	public final static int	EVENT_T_HP				= 2;
	public final static int	EVENT_T_MANA			= 3;
	public final static int	EVENT_T_AGGRO			= 4;
	public final static int	EVENT_T_KILL			= 5;
	public final static int	EVENT_T_DEATH			= 6;
	public final static int	EVENT_T_EVADE			= 7;
	public final static int	EVENT_T_SPELLHIT		= 8;
	public final static int	EVENT_T_RANGE			= 9;
	public final static int	EVENT_T_OOC_LOS			= 10;
	public final static int	EVENT_T_SPAWNED			= 11;
	public final static int	EVENT_T_TARGET_HP		= 12;
	public final static int	EVENT_T_TARGET_CASTING	= 13;
	public final static int	EVENT_T_FRIENDLY_HP		= 14;
	public final static int	EVENT_T_FRIENDLY_IS_CC	= 15;
	public final static int	EVENT_T_MISSING_BUFF	= 16;
	public final static int	EVENT_T_SUMMONED_UNIT	= 17;
	public final static int	EFLAG_REPEATABLE		= 1;
	public final static int EFLAG_NORMAL            = 2;
	public final static int EFLAG_HEROIC			= 4;
	public final static int EFLAG_DEBUG				= 128;

	public static final int	MAX_EVENT_PARAMS		= 4;
	public static final int	MAX_EVENT_ACTIONS		= 3;

	private int				eventId;
	private int				type;
	private long			inversePhaseMask;
	private int[]			param					= new int[MAX_EVENT_PARAMS];
	private Action[]		action					= new Action[MAX_EVENT_ACTIONS];
	private String			comment;
	private int				currentAction			= 0;
	private int				chance					= 100;
	private int				eventFlags;

	public int getEventId() {
		return eventId;
	}

	public void setEventId(int eventId) {
		this.eventId = eventId;
	}

	public void addAction(Action a) {
		a.setEventId(eventId);
		action[currentAction++] = a;
	}

	public int getParam(int n) {
		return param[n];
	}

	public void setParam(int n, int v) {
		param[n] = v;
	}

	public Action getAction(int n) {
		return action[n];
	}

	/**
	 * @return the chance
	 */
	public int getChance() {
		return chance;
	}

	/**
	 * @param chance
	 *            the chance to set
	 */
	public void setChance(int chance) {
		this.chance = chance;
	}

	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * @return the inversePhaseMask
	 */
	public long getInversePhaseMask() {
		return inversePhaseMask;
	}

	/**
	 * @param inversePhaseMask
	 *            the inversePhaseMask to set
	 */
	public void setInversePhaseMask(long inversePhaseMask) {
		this.inversePhaseMask = inversePhaseMask;
	}

	/**
	 * @return the comment
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * @param comment
	 *            the comment to set
	 */
	public void setComment(String comment) {
		this.comment = comment;
	}

	public int getEventFlags() {
		return eventFlags;
	}

	public void setEventFlags(int eventFlags) {
		this.eventFlags = eventFlags;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(action);
		result = prime * result + chance;
		result = prime * result + ((comment == null) ? 0 : comment.hashCode());
		result = prime * result + currentAction;
		result = prime * result + eventFlags;
		result = prime * result + (int) (inversePhaseMask ^ (inversePhaseMask >>> 32));
		result = prime * result + Arrays.hashCode(param);
		result = prime * result + type;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Event other = (Event) obj;
		if (!Arrays.equals(action, other.action))
			return false;
		if (chance != other.chance)
			return false;
		if (comment == null) {
			if (other.comment != null)
				return false;
		} else if (!comment.equals(other.comment))
			return false;
		if (currentAction != other.currentAction)
			return false;
		if (eventFlags != other.eventFlags)
			return false;
		if (inversePhaseMask != other.inversePhaseMask)
			return false;
		if (!Arrays.equals(param, other.param))
			return false;
		if (type != other.type)
			return false;
		return true;
	}

	public void invalidateAction(int f) {
		action[f] = null;		
	}
}
